package com.czz.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.portal.entity.CourseConsult;
import com.czz.portal.query.ConsultQuery;
import com.czz.portal.service.CourseConsultService;
import com.czz.portal.vo.ConsultVo;
import com.czz.portal.vo.OrderConsultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @ClassName: CourseConsultController
 * @Author Czz
 * @Date 2020/2/12
 * @Time 10:04
 **/
@RestController
@RequestMapping(value = "/consult")
public class CourseConsultController {

    @Autowired
    CourseConsultService courseConsultService;

    /**
    * @Description 根据课程id分页查询课程列表
    * @Param [page, limit, consultQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/12
    * @Time 17:50
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getConsultPageList(@PathVariable Long page, @PathVariable Long limit, ConsultQuery consultQuery){
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(consultQuery.getCourseId())){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<ConsultVo> pageParam = new Page<>(page, limit);
        pageParam.setRecords(courseConsultService.getConsultPageList(pageParam, consultQuery));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }

    /**
    * @Description 新增课程咨询
    * @Param [consult]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/12
    * @Time 17:54
    **/
    @PostMapping(value = "")
    public R saveConsult(CourseConsult consult){
        if (ObjectUtils.isEmpty(consult) || StringUtils.isEmpty(consult.getUserId()) || StringUtils.isEmpty(consult.getConsult())){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        consult.setGmtCreate(new Date());
        courseConsultService.save(consult);
        return R.ok();
    }

    /**
    * @Description 获取用户课程咨询分页列表
    * @Param [page, limit, userId]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/15
    * @Time 16:38
    **/
    @GetMapping(value = "/order/{page}/{limit}/{userId}")
    public R getOrderConsultPageList(@PathVariable Long page,@PathVariable Long limit,@PathVariable String userId){
        if (page <= 0 || limit <= 0 || StringUtils.isEmpty(userId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<OrderConsultVo> pageParam = new Page<>(page, limit);
        pageParam.setRecords(courseConsultService.getOrderConsultPageList(pageParam,userId));
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", pageParam.getRecords());
    }
}
